package com.kayak.tg.client.exception;

/**
 * 参数校验失败异常类
 * @author xiamh
 * @date 19/1/3
 */
public class ValidateException extends RuntimeException {

    private String code;		//返回码
    private String msg;			//返回信息

    /**
     * 异常类构造方法
     * @param e			错误码对象
     * @param params	参数
     */
    public ValidateException(ExceptionInterface e, Object... params) {
        this.code = e.getCode();
        if(params != null && params.length > 0) {
            String sb = e.getMsg();
            for(Object str : params) {
                sb = sb.replaceFirst("\\{\\}", String.valueOf(str));
            }
            this.msg = sb;
        }else {
            this.msg = e.getMsg();
        }
    }

    /**
     * 异常类构造方法
     * @param code		返回码
     * @param msg		返回信息
     */
    public ValidateException(String code, String msg, Object... params) {
        this.code = code;
        if(params != null && params.length > 0) {
            String sb = msg;
            for(Object str : params) {
                sb = sb.replaceFirst("\\{\\}", String.valueOf(str));
            }
            this.msg = sb;
        }else {
            this.msg = msg;
        }
    }

    /**
     * 直接传错误信息
     * @param msg
     */
    public ValidateException(String msg, Object... params) {
        this.msg = msg;
        if(params != null && params.length > 0) {
            String sb = msg;
            for(Object str : params) {
                sb = sb.replaceFirst("\\{\\}", String.valueOf(str));
            }
            this.msg = sb;
        }else {
            this.msg = msg;
        }
    }

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String getMessage() {
        return this.toString();
    }


    @Override
    public String toString() {
        return "ValidateException{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                '}';
    }
}
